//
//  CardTableViewHeader.m
//  TSListDemo
//
//  Created by qian on 2020/11/16.
//  Copyright © 2020 dvlproad. All rights reserved.
//

#import "CardTableViewHeader.h"
#import <Masonry/Masonry.h>
#import <CJBaseUIKit/UIColor+CJHex.h>
#import <SDWebImage/SDWebImage.h>

@interface CardTableViewHeader () {
    
}

@end

@implementation CardTableViewHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

#pragma mark - Setter
- (void)setUser:(CQUserDetailModel *)user {
    _user = user;
    
    NSString *imageUrl = user.imageDataModels[0].url;
    NSURL *headerImageURL = [NSURL URLWithString:imageUrl];
    [self.backgroundImageView sd_setImageWithURL:headerImageURL completed:nil];
    
    self.nameLabel.text = user.name;

    NSString *infoMessage = [NSString stringWithFormat:@"%zd · %@ · %@", user.age, user.astro, user.school];
    self.infoLabel.text = infoMessage;
    
    NSString *locationString = [NSString stringWithFormat:@"  %@  ", user.city];
    [self.locationButton setTitle:locationString forState:UIControlStateNormal];
}


#pragma mark - SetupViews & Lazy
- (void)setupViews {
    [self addSubview:self.backgroundImageView];
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self addSubview:self.infoLabel];
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.bottom.equalTo(self).offset(-30);
        make.height.equalTo(@20);
    }];
    
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.infoLabel);
        make.bottom.equalTo(self.infoLabel.mas_top).offset(-7);
        make.height.equalTo(@40);
    }];
    
    [self addSubview:self.locationButton];
    [self.locationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.infoLabel.mas_right).offset(6);
        make.centerY.equalTo(self.infoLabel);
        make.height.equalTo(@18);
    }];
}

- (UIImageView *)backgroundImageView {
    if (_backgroundImageView == nil) {
        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    }
    return _backgroundImageView;
}

- (UILabel *)nameLabel {
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:28];
    }
    return _nameLabel;
}

- (UILabel *)infoLabel {
    if (_infoLabel == nil) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.textColor = [UIColor whiteColor];
        _infoLabel.font = [UIFont systemFontOfSize:14];
    }
    return _infoLabel;
}

- (UIButton *)locationButton {
    if (_locationButton == nil) {
        _locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _locationButton.layer.cornerRadius = 13;
        _locationButton.backgroundColor = CJColorFromHexStringAndAlpha(@"#787878", 0.4);
        [_locationButton.titleLabel setFont:[UIFont systemFontOfSize:10]];
        [_locationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _locationButton;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
